<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var p = Promise.resolve();
        var isLoading = false;
        var sets = new Set();

        function doSomeThing() {
            if (isLoading) {
                return;
            }
            isLoading = true;
            p.then(() => {
                sets.forEach(jog => jog());
                console.log("then")

            }).finally(() => {
                console.log("finally")
                isLoading = false;
            })
        }


        sets.add(() => {
            console.log("1")
        });
        doSomeThing()
        sets.add(() => {
            console.log("2")
        });
        doSomeThing()
        sets.add(() => {
            console.log("3")
        });
        console.log("??")
        doSomeThing()
        sets.add(() => {
            console.log("3")
        });
        console.log("??")
        doSomeThing()
    </script>
</body>

</html>